package com.zhiwen.service.impl;

import com.zhiwen.common.enums.NotificationType;
import com.zhiwen.domain.entity.Question;
import com.zhiwen.domain.entity.User;
import com.zhiwen.service.NotificationTemplateService;
import com.zhiwen.service.QuestionService;
import com.zhiwen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 通知模板服务实现类
 */
@Service
public class NotificationTemplateServiceImpl implements NotificationTemplateService {

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionService questionService;

    @Override
    public String getNotificationTitle(NotificationType type, Map<String, Object> params) {
        switch (type) {
            case QUESTION_ANSWERED:
                return "您的问题有了新回答";
            case ANSWER_COMMENTED:
                return "您的回答收到了新评论";
            case ANSWER_ACCEPTED:
                return "您的回答已被采纳";
            case CONTENT_REVIEWED:
                return "您的内容审核结果";
            case SYSTEM_NOTICE:
                return params.containsKey("title") ? params.get("title").toString() : "系统通知";
            case CONTENT_LIKED:
                return "您的内容获得了点赞";
            case USER_FOLLOWED:
                return "有新用户关注了您";
            default:
                return "智问社区消息";
        }
    }

    @Override
    public String getNotificationContent(NotificationType type, Map<String, Object> params) {
        StringBuilder content = new StringBuilder();

        switch (type) {
            case QUESTION_ANSWERED:
                content.append(getQuestionAnsweredContent(params));
                break;
            case ANSWER_COMMENTED:
                content.append(getAnswerCommentedContent(params));
                break;
            case ANSWER_ACCEPTED:
                content.append(getAnswerAcceptedContent(params));
                break;
            case CONTENT_REVIEWED:
                content.append(getContentReviewedContent(params));
                break;
            case SYSTEM_NOTICE:
                content.append(params.containsKey("content") ? params.get("content").toString() : "系统通知");
                break;
            case CONTENT_LIKED:
                content.append(getContentLikedContent(params));
                break;
            case USER_FOLLOWED:
                content.append(getUserFollowedContent(params));
                break;
            default:
                content.append("您有一条新消息，请查看详情。");
        }

        return content.toString();
    }

    /**
     * 获取问题回答通知内容
     */
    private String getQuestionAnsweredContent(Map<String, Object> params) {
        Long questionId = (Long) params.get("questionId");
        Long senderId = (Long) params.get("senderId");

        // 获取问题信息
        Question question = questionService.getById(questionId);
        String questionTitle = (question != null) ? question.getTitle() : "您的问题";

        // 获取回答者信息
        User answerer = userService.getById(senderId);
        String answererName = (answerer != null) ? answerer.getNickname() : "有用户";

        return answererName + " 回答了您的问题「" + questionTitle + "」，快去看看吧！";
    }

    /**
     * 获取回答评论通知内容
     */
    private String getAnswerCommentedContent(Map<String, Object> params) {
        Long senderId = (Long) params.get("senderId");

        // 获取评论者信息
        User commenter = userService.getById(senderId);
        String commenterName = (commenter != null) ? commenter.getNickname() : "有用户";

        return commenterName + " 评论了您的回答，点击查看详情。";
    }

    /**
     * 获取回答采纳通知内容
     */
    private String getAnswerAcceptedContent(Map<String, Object> params) {
        Long questionId = (Long) params.get("questionId");

        // 获取问题信息
        Question question = questionService.getById(questionId);
        String questionTitle = (question != null) ? question.getTitle() : "某问题";

        return "恭喜您！您的回答已被问题「" + questionTitle + "」的提问者采纳为最佳答案。";
    }

    /**
     * 获取内容审核通知内容
     */
    private String getContentReviewedContent(Map<String, Object> params) {
        Integer contentType = (Integer) params.get("contentType");
        Integer reviewResult = (Integer) params.get("reviewResult");
        String remark = (String) params.get("remark");

        String contentTypeStr;
        switch (contentType) {
            case 1:
                contentTypeStr = "问题";
                break;
            case 2:
                contentTypeStr = "回答";
                break;
            case 3:
                contentTypeStr = "评论";
                break;
            default:
                contentTypeStr = "内容";
        }

        if (reviewResult == 1) {
            return "您的" + contentTypeStr + "已审核通过，现已发布。";
        } else {
            return "您的" + contentTypeStr + "未通过审核。原因：" + remark;
        }
    }

    /**
     * 获取内容点赞通知内容
     */
    private String getContentLikedContent(Map<String, Object> params) {
        Integer contentType = (Integer) params.get("contentType");
        Long senderId = (Long) params.get("senderId");

        // 获取点赞者信息
        User liker = userService.getById(senderId);
        String likerName = (liker != null) ? liker.getNickname() : "有用户";

        String contentTypeStr;
        switch (contentType) {
            case 1:
                contentTypeStr = "问题";
                break;
            case 2:
                contentTypeStr = "回答";
                break;
            case 3:
                contentTypeStr = "评论";
                break;
            default:
                contentTypeStr = "内容";
        }

        return likerName + " 点赞了您的" + contentTypeStr + "，感谢您的分享！";
    }

    /**
     * 获取用户关注通知内容
     */
    private String getUserFollowedContent(Map<String, Object> params) {
        Long senderId = (Long) params.get("senderId");

        // 获取关注者信息
        User follower = userService.getById(senderId);
        String followerName = (follower != null) ? follower.getNickname() : "有用户";

        return followerName + " 关注了您，点击查看Ta的主页。";
    }
}
